package xx.pro.completableFuture;

import java.util.concurrent.CompletableFuture;
import java.util.concurrent.ExecutionException;
import java.util.concurrent.ExecutorService;
import java.util.concurrent.Executors;

/**
 * @Classname Demo3
 * @Description TODO
 * @Date 2023/11/4 14:02
 * @Created by wangxianlin
 */
public class Demo05 {

    static ExecutorService fixedThreadPool = Executors.newFixedThreadPool(6);

    public static void main(String[] args) throws ExecutionException, InterruptedException {

        CompletableFuture<Integer> future1 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务1 子线程执行" + Thread.currentThread().getName());
            try {
                Thread.sleep(1000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 11;
        }, fixedThreadPool);


        CompletableFuture<Integer> future2 = CompletableFuture.supplyAsync(() -> {
            System.out.println("任务2 子线程执行" + Thread.currentThread().getName());
            try {
                Thread.sleep(4000);
            } catch (InterruptedException e) {
                e.printStackTrace();
            }
            return 13;
        }, fixedThreadPool);


        future1.runAfterEither(future2,()->{
            System.out.println(Thread.currentThread().getName());
        });

        CompletableFuture<Integer> resFuture = future1.thenCombine(future2,(res1,res2)->{
            System.out.println(Thread.currentThread().getName());
            return res1 + res2;
        });
    }
}
